package org.lx.nursing.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lx.common.constant.HttpStatus;
import org.lx.common.core.page.TableDataInfo;
import org.lx.nursing.domain.Elder;
import org.lx.nursing.mapper.ElderMapper;
import org.lx.nursing.service.IElderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService {
    @Autowired
    private ElderMapper elderMapper;

    @Override
    public Elder selectElderById(Long id) {
        return getById(id);
    }

    @Override
    public List<Elder> selectElderList(Elder elder) {
        return elderMapper.selectElderList(elder);
    }

    @Override
    public int insertElder(Elder elder) {
        return save(elder) ? 1 : 0;
    }

    @Override
    public int updateElder(Elder elder) {
        return updateById(elder) ? 1 : 0;
    }

    @Override
    public int deleteElderByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    @Override
    public int deleteElderById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List pageQuery(Integer status, Integer pageNum, Integer pageSize, Elder elder) {
        //创建条件构建器
        LambdaQueryWrapper<Elder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Elder::getStatus, status)
                .like(StrUtil.isNotBlank(elder.getName()), Elder::getName, elder.getName())
                .like(StrUtil.isNotEmpty(elder.getIdCardNo()), Elder::getIdCardNo, elder.getIdCardNo());


        //sql返回结果，只展示主键、名字、身份照号、床位编号
        queryWrapper.select(Elder::getId, Elder::getName, Elder::getIdCardNo, Elder::getBedNumber);
        Page<Elder> page = new Page<>(pageNum, pageSize);
        page = page(page, queryWrapper);

        return page.getRecords();
    }
}
